package org.shipp.adapter;

import java.util.LinkedList;
import java.util.List;

import org.shipp.activity.PrincipalActivity;
import org.shipp.activity.R;
import org.shipp.model.Item;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * 
 * @author Efren
 *
 */
public class MyListAdapter extends BaseExpandableListAdapter {

	private Context context;
	private LinkedList<Item> continentList;
	private LinkedList<Item> originalList;
	private Display display;

	public MyListAdapter(Context context, LinkedList<Item> menuList) {
		this.context = context;
		this.continentList = new LinkedList<Item>();
		this.originalList = new LinkedList<Item>();

		this.continentList.addAll(menuList);
		this.originalList.addAll(menuList);
		
		display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		List<Item> countryList = continentList.get(groupPosition).getItems();
		return countryList.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {

		Item country = (Item) getChild(groupPosition, childPosition);
		if (view == null) {
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.menu_child_row, null);
		}
		
		if(country.getLink() == null || country.getLink().equalsIgnoreCase("")){
			view.setBackgroundColor(Color.parseColor("#002D38"));
			view.setPadding(0, 0, 0, 0);
		}else{
			view.setBackgroundResource(R.drawable.list_selector_children_com_link);
			view.setPadding(25, 0, 0, 0);
		}

		TextView code = (TextView) view.findViewById(R.id.titulo);
		code.setText(country.getTitulo());

        int left = (int) (display.getWidth() - (display.getWidth() * PrincipalActivity.INDICE_PORCENTAGEM_TELA_PADRAO));
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(code.getLayoutParams());
        params.rightMargin = left;
        code.setLayoutParams(params);
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {

		LinkedList<Item> countryList = continentList.get(groupPosition).getItems();
		return countryList.size();

	}

	@Override
	public Object getGroup(int groupPosition) {
		return continentList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return continentList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {

		Item continent = (Item) getGroup(groupPosition);
		if (view == null) {
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.menu_group_row, null);
		}

		TextView heading = (TextView) view.findViewById(R.id.titulo);
		heading.setText(continent.getTitulo());

		return view;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void filterData(String query) {

		query = query.toLowerCase();
		Log.v("MyListAdapter", String.valueOf(continentList.size()));
		continentList.clear();

		if (query == null || "".equalsIgnoreCase(query)) {
			continentList.addAll(originalList);
		} else {

			for (Item continent : originalList) {

				LinkedList<Item> countryList = continent.getItems();
				LinkedList<Item> newList = new LinkedList<Item>();
				for (Item country : countryList) {
					if ((country.getTitulo().toLowerCase().contains(query) || country.getTitulo().toLowerCase().contains(query)) && country.getLink() != null && !"".equalsIgnoreCase(country.getLink()) ) {
						newList.add(country);
					}
				}
				if (newList.size() > 0) {
					Item nContinent = new Item(continent.getTitulo(), newList);
					continentList.add(nContinent);
				}
			}
		}

		Log.v("MyListAdapter", String.valueOf(continentList.size()));
		notifyDataSetChanged();

	}

}